package com.block.im.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.ImUserBaseInfo;
import com.block.im.enums.UserTypeEnum;
import com.block.im.model.dto.IMHttpResultDTO;
import com.block.im.model.dto.IMUserInfoDTO;
import com.block.im.model.param.ImUserParam;
import com.block.im.service.IImBaseCommonService;
import com.block.im.service.IImUserBaseInfoService;
import com.block.im.service.IImUserSigInfoService;
import com.block.im.service.IMsgHandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MsgHandleServiceImpl implements IMsgHandleService {

    @Autowired
    IImBaseCommonService imMsgCommonService;
    @Autowired
    IImUserSigInfoService imUserSigInfoService;
    @Autowired
    IImUserBaseInfoService imUserBaseInfoService;

    @Override
    public ResponseVo loginIn(String currUserId, boolean fulshFlag) {

        ImUserParam fromUser = new ImUserParam(currUserId, UserTypeEnum.BUS);
        IMUserInfoDTO imUserInfoDTO = imUserSigInfoService.genUserSig(fromUser, fulshFlag);

        JSONObject returnObject = new JSONObject();
        returnObject.put("userId", imUserInfoDTO.getUserId());
        returnObject.put("userSig", imUserInfoDTO.getUserSig());

        return new ResponseVo(AppRspCodeConstant.SUCCESS, returnObject);
    }

//    @Override
//    public ResponseVo getSessionList(String currUserId, Integer startIndex) {
//
//        ImUserParam fromUser = new ImUserParam(currUserId, UserTypeEnum.BUS);
//        IMHttpResultDTO imHttpResultDTO = imMsgCommonService.getSessionList(fromUser, startIndex);
//
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("success", imHttpResultDTO.isSuccess());
//        if (imHttpResultDTO.isSuccess()){
//            jsonObject.putAll(imHttpResultDTO.getResultObj());
//        }
//
//        return new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
//    }
//
//    @Override
//    public ResponseVo getHistoryMsg(String currUserId, String peerImUserId, String lastMsgKey) {
//
//        ImUserParam mainUser = new ImUserParam(currUserId, UserTypeEnum.BUS);
//        ImUserParam peerUser = new ImUserParam(peerImUserId);
//
//        IMHttpResultDTO imHttpResultDTO = imMsgCommonService.getHistoryMsg(mainUser, peerUser, lastMsgKey);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("success", imHttpResultDTO.isSuccess());
//        if (imHttpResultDTO.isSuccess()){
//            jsonObject.putAll(imHttpResultDTO.getResultObj());
//        }
//
//        return new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
//    }

    @Override
    public ResponseVo toSayHello(String loginUid, String toUserId, String msgContent) {

        ImUserParam adminUserParam = new ImUserParam(loginUid, UserTypeEnum.BUS);
        ImUserParam toUserParam = new ImUserParam(toUserId, UserTypeEnum.USER);

        boolean b = this.sendMsg(adminUserParam, toUserParam, msgContent);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success",  b);
        return new ResponseVo(AppRspCodeConstant.SUCCESS, jsonObject);
    }


    @Override
    public boolean sendMsg(ImUserParam fromUser, ImUserParam toUser, String msgContent) {

        ImUserBaseInfo imUserInfo = imUserBaseInfoService.getImUserInfo(fromUser);
        ImUserBaseInfo imUserInfo1 = imUserBaseInfoService.getImUserInfo(toUser);
        if (ObjectUtil.hasEmpty(imUserInfo, imUserInfo1)){
            return false;
        }

        IMHttpResultDTO imHttpResultDTO = imMsgCommonService.sendSingleMsgInfo(fromUser, toUser, msgContent);
        return  imHttpResultDTO.isSuccess();
    }

}
